home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog7.arj / SYSCOLOR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  16.4 KB  |  605 lines

  1. { syscolor.pas -- Set system colors }
  2.  
  3. program SysColor;
  4.  
  5. {$R syscolor.res }
  6.  
  7. uses WinTypes, WinProcs, WObjects, Strings;
  8.  
  9. const
  10.  
  11.   app_Name  = 'SysColor';       { Application name }
  12.   ini_FName = 'SYSCOLOR.INI';   { .INI file name }
  13.  
  14.   id_Menu      = 100;           { Menu resource ID }
  15.   id_Icon      = 200;           { Icon resource ID }
  16.   cm_About     = 101;           { Menu:About command resource ID }
  17.   cm_Quit      = 102;           { Menu:Exit command resource ID }
  18.   id_SBarRed   = 100;           { Window control IDs }
  19.   id_SBarGrn   = 101;
  20.   id_SBarBlu   = 102;
  21.   id_SetBtn    = 103;
  22.   id_ResetBtn  = 104;
  23.   id_SaveBtn   = 105;
  24.   id_QuitBtn   = 106;
  25.  
  26.   RedMask = $000000FF;          { Color value extraction masks }
  27.   GrnMask = $0000FF00;
  28.   BluMask = $00FF0000;
  29.  
  30.   nonStop: Boolean = false;     { Use switches: -s = false; -n = true }
  31.  
  32. type
  33.  
  34.   SCApplication = object(TApplication)
  35.     constructor Init(AName: PChar);
  36.     procedure InitMainWindow; virtual;
  37.   end;
  38.  
  39.   PColorScrollBar = ^TColorScrollBar;
  40.   TColorScrollBar = object(TScrollBar)
  41.     Digits: PStatic;
  42.     constructor Init(AParent: PWindowsObject; AnID, Y: Integer;
  43.       ALabel: PChar; var SText: PStatic);
  44.     procedure SetupWindow; virtual;
  45.     procedure DefNotificationProc(var Msg: TMessage); virtual;
  46.     procedure SetDigits;
  47.     procedure SetPosition(ThumbPos: Integer); virtual;
  48.   end;
  49.  
  50.   PSCWindow = ^SCWindow;
  51.   SCWindow = object(TWindow)
  52.   {- Data fields }
  53.     Dc: Hdc;
  54.     ButtonDown, Changed: Boolean;
  55.     LineX1, LineY1, LineX2, LineY2: Integer;
  56.     ArrowCursor, CrossHairCursor: HCursor;
  57.     SBarRed, SBarGrn, SBarBlu: PColorScrollBar;
  58.     STxtRed, STxtGrn, STxtBlu: PStatic;
  59.     SampleRect: TRect;
  60.     SampleColor: TColorRef;
  61.     DraggingOrigin: Integer;
  62.   {- Methods }
  63.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  64.     destructor Done; virtual;
  65.     procedure SetupWindow; virtual;
  66.     procedure InitChildControls;
  67.     procedure ResetSystemColors;
  68.     procedure SynchronizeScrollBars;
  69.     procedure DrawRubberband;
  70.     function CanClose: Boolean; virtual;
  71.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  72.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  73.     function InsideColorRect(X, Y: Integer; var Index: Integer): Boolean;
  74.     procedure CMAbout(var Msg: TMessage);
  75.       virtual cm_First + cm_About;
  76.     procedure CMQuit(var Msg: TMessage);
  77.       virtual cm_First + cm_Quit;
  78.     procedure WMHScroll(var Msg: TMessage);
  79.       virtual wm_First + wm_HScroll;
  80.     procedure WMLButtonDown(var Msg: TMessage);
  81.       virtual wm_First + wm_LButtonDown;
  82.     procedure WMLButtonUp(var Msg: TMessage);
  83.       virtual wm_First + wm_LButtonUp;
  84.     procedure WMMouseMove(var Msg: TMessage);
  85.       virtual wm_First + wm_MouseMove;
  86.     procedure SaveBtnEvent(var Msg: TMessage);
  87.       virtual id_First + id_SaveBtn;
  88.     procedure SetBtnEvent(var Msg: TMessage);
  89.       virtual id_First + id_SetBtn;
  90.     procedure QuitBtnEvent(var Msg: TMessage);
  91.       virtual id_First + id_QuitBtn;
  92.     procedure ResetBtnEvent(var Msg: TMessage);
  93.       virtual id_First + id_ResetBtn;
  94.   end;
  95.  
  96.   SysColorRec = record
  97.     OriginalColor: TColorRef;  { Color on starting program }
  98.     CurrentColor: TColorRef;   { New color selected by user }
  99.     SCRect: TRect;             { Location of system-color rectangle }
  100.   end;
  101.  
  102. var
  103.  
  104.   SysColorArray: Array[0 .. color_EndColors] of SysColorRec;
  105.   SysColorNames: Array[0 .. color_EndColors] of PChar;
  106.  
  107.  
  108. { Common routines }
  109.  
  110. {- Convert integer N to C char array. If Max > 0, pad with 0s. }
  111. procedure Int2Str(N, Max: Integer; C: PChar);
  112. var
  113.   S: String[6];
  114. begin
  115.   Str(N, S);
  116.   while Length(S) < Max do S := '0' + S;
  117.   StrPCopy(C, S)
  118. end;
  119.  
  120. {- Prepare global SysColorArray with current color values }
  121. procedure InitSysColorArray;
  122. var
  123.   I: Integer;
  124. begin
  125.   for I := 0 to color_EndColors do with SysColorArray[I] do
  126.   begin
  127.     OriginalColor := GetSysColor(I);
  128.     CurrentColor := OriginalColor;
  129.     with SCRect do
  130.     begin
  131.       Left := 500;
  132.       Top := 20 + (I * 20);
  133.       Right := 600;
  134.       Bottom := Top + 15
  135.     end
  136.   end
  137. end;
  138.  
  139. {- Change system colors to values in SysColorArray }
  140. procedure ChangeSystemColors;
  141. var
  142.   I: Integer;
  143.   InxArray: Array[0 .. color_EndColors] of Integer;
  144.   ClrArray: Array[0 .. color_EndColors] of TColorRef;
  145. begin
  146.   for I := 0 to color_EndColors do
  147.   begin
  148.     InxArray[I] := I;
  149.     ClrArray[I] := SysColorArray[I].CurrentColor
  150.   end;
  151.   SetSysColors(color_EndColors + 1, InxArray[0], ClrArray[0])
  152. end;
  153.  
  154. {- Save colors to SYSCOLOR.INI in Windows directory }
  155. function SaveSettings: Boolean;
  156. var
  157.   I: Integer;
  158.   S: String[12];
  159.   NewValue: array[0 .. 12] of Char;
  160. begin
  161.   SaveSettings := true;  { Think positively! }
  162.   for I := 0 to color_EndColors do with SysColorArray[I] do
  163.   begin
  164.     Str(CurrentColor, S);
  165.     StrPCopy(NewValue, S);
  166.     if not WritePrivateProfileString(app_Name, SysColorNames[I],
  167.       NewValue, ini_FName) then
  168.     begin
  169.       SaveSettings := false;
  170.       Exit
  171.     end
  172.   end
  173. end;
  174.  
  175. {- Load colors from SYSCOLOR.INI if present }
  176. procedure LoadSettings;
  177. var
  178.   I, Err: Integer;
  179.   S: String[12];
  180.   DefaultValue, NewValue: array[0 .. 12] of Char;
  181. begin
  182.   for I := 0 to color_EndColors do with SysColorArray[I] do
  183.   begin
  184.     Str(CurrentColor, S);
  185.     StrPCopy(DefaultValue, S);
  186.     GetPrivateProfileString(app_Name, SysColorNames[I],
  187.       DefaultValue, NewValue, sizeof(NewValue), ini_FName);
  188.     S := StrPas(NewValue);
  189.     Val(S, CurrentColor, Err);
  190.     if Err <> 0 then CurrentColor := OriginalColor
  191.   end;
  192.   GetPrivateProfileString(app_Name, 'nonstop',
  193.     'false', NewValue, sizeof(NewValue), ini_FName);
  194.   if StrComp('false', NewValue) <> 0
  195.     then nonStop := true
  196. end;
  197.  
  198. {- Get command-line switches }
  199. procedure GetSwitches;
  200. var
  201.   I: Integer;
  202.   S: String[128];
  203.   C: Char;
  204. begin
  205.   for I := 1 to ParamCount do
  206.   begin
  207.     S := ParamStr(I);
  208.     C := upcase(S[1]);
  209.     if (Length(S) > 1) and ((C = '-') or (C = '/')) then
  210.     case upcase(S[2]) of
  211.       'N' : nonStop := true;
  212.       'S' : nonStop := false
  213.     end
  214.   end
  215. end;
  216.  
  217.  
  218. { SCApplication }
  219.  
  220. {- Construct SCApplication object }
  221. constructor SCApplication.Init(AName: PChar);
  222. begin
  223.   TApplication.Init(AName);
  224.   InitSysColorArray;          { Initialize colors }
  225.   LoadSettings;               { Load .INI settings if present }
  226.   GetSwitches;                { Get command-line switches }
  227.   if nonStop then
  228.   begin                       { Optional nonstop operation: }
  229.     ChangeSystemColors;       { Change colors to .INI settings }
  230.     MainWindow^.CloseWindow   { Exit application }
  231.   end;
  232. end;
  233.  
  234. {- Initialize application's window }
  235. procedure SCApplication.InitMainWindow;
  236. begin
  237.   MainWindow := New(PSCWindow, Init(nil, 'Set System Colors'))
  238. end;
  239.  
  240.  
  241. { TColorScrollBar }
  242.  
  243. {- Construct TColorScrollBar instance }
  244. constructor TColorScrollBar.Init(AParent: PWindowsObject;
  245.   AnID, Y: Integer; ALabel: PChar; var SText: PStatic);
  246. begin
  247.   TScrollBar.Init(AParent, AnID, 50, Y, 250, 18, true);
  248.   New(SText, Init(AParent, -1, ALabel, 5, Y, 40, 18, 40));
  249.   New(Digits, Init(AParent, -1, '000', 310, Y, 40, 18, 3));
  250.   SText := Digits  { Return pointer to control's digital "readout" }
  251. end;
  252.  
  253. {- Set scroll bar range for a byte color value }
  254. procedure TColorScrollBar.SetupWindow;
  255. begin
  256.   TScrollBar.SetupWindow;
  257.   SetRange(0, 255)
  258. end;
  259.  
  260. {- Change digital readout for changes to scroll bar position }
  261. procedure TColorScrollBar.DefNotificationProc(var Msg: TMessage);
  262. begin
  263.   SetDigits;
  264.   TScrollBar.DefNotificationProc(Msg)
  265. end;
  266.  
  267. {- Change digital readout to match current position }
  268. procedure TColorScrollBar.SetDigits;
  269. var
  270.   C: array[0 .. 3] of Char;
  271. begin
  272.   Int2Str(GetPosition, 3, C);
  273.   Digits^.SetText(C)
  274. end;
  275.  
  276. {- Force scroll bar to specific position }
  277. procedure TColorScrollBar.SetPosition(ThumbPos: Integer);
  278. begin
  279.   TScrollBar.SetPosition(ThumbPos);
  280.   SetDigits
  281. end;
  282.  
  283.  
  284. { SCWindow }
  285.  
  286. {- Construct SCWindow object and instantiate child windows }
  287. constructor SCWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  288. begin
  289.   TWindow.Init(AParent, ATitle);
  290.   EnableKBHandler;
  291.   Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
  292.   with Attr do
  293.   begin
  294.     X := 10; Y := 10; H := 460; W := 615
  295.   end;
  296.   ButtonDown := false;
  297.   Changed := false;
  298.   ArrowCursor := LoadCursor(0, idc_Arrow);
  299.   CrossHairCursor := LoadCursor(0, idc_Cross);
  300.   SampleColor := 0;
  301.   with SampleRect do
  302.   begin
  303.     Left := 200; Top := 150; Right := 300; Bottom := 230;
  304.   end;
  305.   InitChildControls
  306. end;
  307.  
  308. {- Finish window preparation }
  309. procedure SCWindow.SetupWindow;
  310. var
  311.   I: Integer;
  312.   Buffer: array[0 .. 40] of Char;
  313. begin
  314.   TWindow.SetupWindow;
  315.   SetFocus(ChildList^.HWindow);
  316.   for I := 0 to color_EndColors do
  317.   begin
  318.     LoadString(HInstance, I, Buffer, 40);
  319.     SysColorNames[I] := StrNew(Buffer)
  320.   end
  321. end;
  322.  
  323. {- Destroy SCWindow instance }
  324. destructor SCWindow.Done;
  325. var
  326.   I: Integer;
  327. begin
  328.   for I := 0 to color_EndColors do
  329.     StrDispose(SysColorNames[I]);
  330.   TWindow.Done
  331. end;
  332.  
  333. {- Create and initialize child controls in window }
  334. procedure SCWindow.InitChildControls;
  335. var
  336.   AControl: PControl;  { Throw-away control pointer }
  337. begin
  338.   SBarRed := New(PColorScrollBar, Init(@Self, id_SBarRed, 20,
  339.     'Red', STxtRed));
  340.   SBarGrn := New(PColorScrollBar, Init(@Self, id_SBarGrn, 60,
  341.     'Green', STxtGrn));
  342.   SBarBlu := New(PColorScrollBar, Init(@Self, id_SBarBlu, 100,
  343.     'Blue', STxtBlu));
  344.   AControl    := New(PButton, Init(@Self, id_SetBtn,
  345.     'Se&t',   50, 150, 80, 40, false));
  346.   AControl    := New(PButton, Init(@Self, id_ResetBtn,
  347.     '&Reset', 50, 210, 80, 40, false));
  348.   AControl    := New(PButton, Init(@Self, id_SaveBtn,
  349.     '&Save',  50, 270, 80, 40, false));
  350.   AControl    := New(PButton, Init(@Self, id_QuitBtn,
  351.     '&Quit',  50, 330, 80, 40, true))
  352. end;
  353.  
  354. {- Return true if window may close }
  355. function SCWindow.CanClose: Boolean;
  356. var
  357.   Answer: Integer;
  358. begin
  359.   CanClose := true;
  360.   if Changed then
  361.   begin
  362.     Answer := MessageBox(HWindow, 'Save colors before quitting?',
  363.       'Please answer', mb_YesNoCancel or mb_IconQuestion);
  364.     if Answer = idYes then
  365.       CanClose := SaveSettings
  366.     else if Answer = idCancel then
  367.       CanClose := false
  368.   end
  369. end;
  370.  
  371. {- Reset system colors to values saved at start of program }
  372. procedure SCWindow.ResetSystemColors;
  373. var
  374.   I: Integer;
  375. begin
  376.   for I := 0 to color_EndColors do with SysColorArray[I] do
  377.     CurrentColor := OriginalColor;
  378.   Changed := false
  379. end;
  380.  
  381. {- Modify window class to use custom icon }
  382. procedure SCWindow.GetWindowClass(var AWndClass: TWndClass);
  383. begin
  384.   TWindow.GetWindowClass(AWndClass);
  385.   AWndClass.hIcon := LoadIcon(HInstance, PChar(id_Icon))
  386. end;
  387.  
  388. {- Adjust scroll bars to match SampleColor }
  389. procedure SCWindow.SynchronizeScrollBars;
  390. var
  391.   DummyMsg: TMessage;
  392. begin
  393.   SBarRed^.SetPosition(SampleColor and RedMask);
  394.   SBarGrn^.SetPosition((SampleColor and GrnMask) shr 8);
  395.   SBarBlu^.SetPosition((SampleColor and BluMask) shr 16);
  396.   WMHScroll(DummyMsg)  { Force scroll bar update }
  397. end;
  398.  
  399. {- Display "About program" dialog box }
  400. procedure SCWindow.CMAbout(var Msg: TMessage);
  401. var
  402.   Dialog: TDialog;
  403. begin
  404.   Dialog.Init(@Self, 'About');
  405.   Dialog.Execute;
  406.   Dialog.Done
  407. end;
  408.  
  409. {- Execute Menu:Exit command }
  410. procedure SCWindow.CMQuit(var Msg: TMessage);
  411. begin
  412.   CloseWindow
  413. end;
  414.  
  415. {- Draw rubberband connecting line while dragging colors }
  416. procedure SCWindow.DrawRubberband;
  417. begin
  418.   MoveTo(Dc, LineX1, LineY1);
  419.   LineTo(Dc, LineX2, LineY2)
  420. end;
  421.  
  422. {- Return true if point X, Y is inside a color rectangle }
  423. function SCWindow.InsideColorRect(X, Y: Integer;
  424.   var Index: Integer): Boolean;
  425. var
  426.   CursorLocation: TPoint;
  427.   I: Integer;
  428. begin
  429.   CursorLocation.X := X;
  430.   CursorLocation.Y := Y;
  431.   InsideColorRect := true;
  432.   if PtInRect(SampleRect, CursorLocation) then
  433.   begin
  434.     Index := -1;      { Inside sample color box }
  435.     Exit
  436.   end else
  437.   for I := 0 to color_EndColors do
  438.     if PtInRect(SysColorArray[I].SCRect, CursorLocation) then
  439.     begin
  440.       Index := I;     { Inside a system color rectangle }
  441.       Exit
  442.     end;
  443.   InsideColorRect := false
  444. end;
  445.  
  446. {- Change color rectangle when a scroll bar moves }
  447. procedure SCWindow.WMHScroll(var Msg: TMessage);
  448. begin
  449.   TWindow.WMHScroll(Msg);
  450.   SampleColor := RGB(SBarRed^.GetPosition, SBarGrn^.GetPosition,
  451.     SBarBlu^.GetPosition);
  452.   InvalidateRect(HWindow, @SampleRect, False)
  453. end;
  454.  
  455. {- Handle left-button down event }
  456. procedure SCWindow.WMLButtonDown(var Msg: TMessage);
  457. begin
  458.   if not ButtonDown then with Msg do
  459.   if InsideColorRect(LParamLo, LParamHi, DraggingOrigin) then
  460.   begin
  461.     Dc := GetDC(HWindow);
  462.     LineX1 := LParamLo;
  463.     LineY1 := LParamHi;
  464.     LineX2 := LineX1;
  465.     LineY2 := LineY1;
  466.     SetROP2(Dc, r2_Not);
  467.     DrawRubberband;
  468.     ButtonDown := true;
  469.     SetCursor(CrossHairCursor);
  470.     SetCapture(HWindow);
  471.     if DraggingOrigin >= 0 then {- Clicked in a system color rect }
  472.     begin
  473.       SampleColor := SysColorArray[DraggingOrigin].CurrentColor;
  474.       SynchronizeScrollBars
  475.     end
  476.   end
  477. end;
  478.  
  479. {- Handle left-button up event }
  480. procedure SCWindow.WMLButtonUp(var Msg: TMessage);
  481. var
  482.   Index: Integer;
  483.   NewColor: TColorRef;
  484. begin
  485.   if ButtonDown then with Msg do
  486.   begin
  487.     if InsideColorRect(LParamLo, LParamHi, Index) then
  488.     if (Index <> DraggingOrigin) and (Index >= 0) then
  489.     begin
  490.       Changed := true;
  491.       if DraggingOrigin >= 0
  492.         then NewColor := SysColorArray[DraggingOrigin].CurrentColor
  493.         else NewColor := SampleColor;
  494.       SysColorArray[Index].CurrentColor := NewColor;
  495.       InvalidateRect(HWindow, nil, False)
  496.     end;
  497.     DrawRubberband;         { Erase last line }
  498.     SetROP2(Dc, r2_Black);
  499.     ButtonDown := false;
  500.     SetCursor(ArrowCursor);
  501.     ReleaseDC(HWindow, Dc);
  502.     ReleaseCapture
  503.   end
  504. end;
  505.  
  506. {- Handle mouse-move event }
  507. procedure SCWindow.WMMouseMove(var Msg: TMessage);
  508. begin
  509.   if ButtonDown then
  510.   begin
  511.     DrawRubberband;         { Erase old line }
  512.     with Msg do
  513.     begin
  514.       LineX2 := LParamLo;
  515.       LineY2 := LParamHi;
  516.       DrawRubberband        { Draw new line }
  517.     end
  518.   end
  519. end;
  520.  
  521. {- Respond to Set button selection }
  522. procedure SCWindow.SetBtnEvent(var Msg: TMessage);
  523. begin
  524.   ChangeSystemColors
  525. end;
  526.  
  527. {- Respond to Reset button selection }
  528. procedure SCWindow.ResetBtnEvent(var Msg: TMessage);
  529. begin
  530.   ResetSystemColors;
  531.   ChangeSystemColors
  532. end;
  533.  
  534. {- Respond to Save button selection }
  535. procedure SCWindow.SaveBtnEvent(var Msg: TMessage);
  536. begin
  537.   if SaveSettings then Changed := false
  538. end;
  539.  
  540. {- Respond to Quit button selection }
  541. procedure SCWindow.QuitBtnEvent(var Msg: TMessage);
  542. begin
  543.   CloseWindow
  544. end;
  545.  
  546. {- Paint window contents }
  547. procedure SCWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  548. var
  549.   OldBrush, TheBrush: HBrush;
  550.   I: Integer;
  551.  
  552.   procedure ShowSysColor(I: Integer);
  553.   var
  554.     SysColorBrush : HBrush;
  555.     OldBrush: HBrush;
  556.     SCName : PChar;
  557.   begin
  558.     with SysColorArray[I], SCRect do
  559.     begin
  560.       SysColorBrush := CreateSolidBrush(CurrentColor);
  561.       OldBrush := SelectObject(PaintDC, SysColorBrush);
  562.       Rectangle(PaintDC, Left, Top, Right, Bottom);
  563.       SelectObject(PaintDC, OldBrush);
  564.       DeleteObject(SysColorBrush);
  565.       SCName := SysColorNames[I];
  566.       TextOut(PaintDC, Left - 125, Top, SCName, StrLen(SCName))
  567.     end
  568.   end;
  569.  
  570. begin
  571.   TheBrush := CreateSolidBrush(SampleColor);
  572.   OldBrush := SelectObject(PaintDC, TheBrush);
  573.   with SampleRect do Rectangle(PaintDC, Left, Top, Right, Bottom);
  574.   SelectObject(PaintDC, OldBrush);
  575.   DeleteObject(TheBrush);
  576.   for I := 0 to color_EndColors do
  577.     ShowSysColor(I)
  578. end;
  579.  
  580. var
  581.  
  582.   SCApp: SCApplication;
  583.  
  584. begin
  585.   SCApp.Init(app_Name);
  586.   SCApp.Run;
  587.   SCApp.Done
  588. end.
  589.  
  590.  
  591. { --------------------------------------------------------------
  592.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  593.   Revision 1.00    Date: 2/1/1991
  594.   Revision 1.01    Date: 2/27/1991
  595.   1. Changed all cm_Exit constants to cm_Quit
  596.   2. Changed all CMExit procedure names to CMQuit
  597.   3. Added length argument to all TStatic object inits
  598.   Revision 1.02    Date: 5/11/1991
  599.   1. Changed all PostQuitMessage calls to CloseWindow
  600.   2. Added TColorScrollBar object
  601.   3. Enabled (limited) keyboard use of controls
  602.   4. Added hot key letters to buttons
  603.   5. Moved most string constants to string table resource
  604.   ------------------------------------------------------------- }
  605.